﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Validation;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Edwon.Biz;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Content.Links {
    public partial class Edit : PageSupport {
        String EOKey = null;
        IBiz biz = BizFactory.Get<IBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm(EOKey);
                }
            }
        }

        void InitForm() {
            this.txt_StampTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        void FillForm(String key) {
            VNewsEditEO eo = biz.Load<VNewsEditEO>(key, this.OwnerKey);
            this.txt_Title.Text = eo.Title;
            this.txt_StampTime.Text = ClientBiz.ParseNullDt2String(eo.StampTime);
            this.txt_Url.Text = eo.Url;
            if (String.IsNullOrEmpty(eo.Thumbnail)) {
                this.UploadPriviewer.Width = 0;
                this.UploadPriviewer.Height = 0;
            } else {
                this.UploadPriviewer.ImageUrl = SiteConfig.Upload.UploadUrl + eo.Thumbnail;
            }
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_Title.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            if (String.IsNullOrEmpty(EOKey)) {
                TNewsEO eo = new TNewsEO();
                eo.LastDate = DateTime.Now;
                eo.Content = "";
                eo.Poster = (this.Actor == null) ? "" : this.Actor.UserName;
                eo.Title = this.txt_Title.Text;
                eo.OwnerKey = this.OwnerKey;
                eo.ListKey = CodeTypeDef.NewsList.NewslistLinks;
                try {
                    eo.StampTime = Convert.ToDateTime(this.txt_StampTime.Text);
                } catch {
                    eo.StampTime = DateTime.Now;
                }
                eo.Url = this.txt_Url.Text;
                String key = (String)biz.Save<TNewsEO>(eo);

                //保存图片
                if (!String.IsNullOrEmpty(this.file_Upload.Value)) {
                    TUploadFileEO feo = new TUploadFileEO();
                    ClientBiz.UploadFile(ref feo, Request.Files[0], CodeTypeDef.Module.Newslist, key, this.OwnerKey, this.Actor);
                    biz.Save<TUploadFileEO>(feo);
                }
                BizFactory.Get<ILogBiz>().LogInsert("新闻列表", eo.Title, this.Actor, Request);
            } else {
                TNewsEO eo = biz.Load<TNewsEO>(EOKey, this.OwnerKey);
                eo.LastDate = DateTime.Now;
                eo.Poster = (this.Actor == null) ? "" : this.Actor.UserName;
                eo.Title = this.txt_Title.Text;
                eo.Url = this.txt_Url.Text;
                try {
                    eo.StampTime = Convert.ToDateTime(this.txt_StampTime.Text);
                } catch {
                }
                //保存图片
                if (!String.IsNullOrEmpty(this.file_Upload.Value)) {
                    //删除原来的图片
                    ClientBiz.DeleteFileByModelKey(eo.NewsKey,this.OwnerKey);
                    TUploadFileEO feo = new TUploadFileEO();
                    ClientBiz.UploadFile(ref feo, Request.Files[0], CodeTypeDef.Module.Newslist, eo.NewsKey, this.OwnerKey, this.Actor);
                    biz.Save<TUploadFileEO>(feo);
                }
                biz.Update<TNewsEO>(eo);
                
                BizFactory.Get<ILogBiz>().LogUpdate("新闻列表", eo.Title, this.Actor, Request);
            }
            Response.Redirect("List.aspx");
        }
    }
}
